package com.example.myone.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Activity.ArticleActivity;
import com.example.myone.Activity.AuthorActivity;
import com.example.myone.Entity.Article;
import com.example.myone.Entity.User;
import com.example.myone.Mapper.UserMapper;
import com.example.myone.R;
import com.example.myone.Util.ClickUtils;
import com.example.myone.Util.ImageUtils;
import com.google.android.material.card.MaterialCardView;

import java.util.List;
import java.util.Objects;

public class DiscoverArticleAdapter extends RecyclerView.Adapter<DiscoverArticleAdapter.ArticleViewHolder> {
    private List<Article> articleList;
    private final Context context;

    public DiscoverArticleAdapter(Context context, List<Article> articleList) {
        this.context = context;
        this.articleList = articleList;
    }

    @NonNull
    @Override
    public ArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.discover_pager_article_item, parent, false);
        return new ArticleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ArticleViewHolder holder, int position) {
        Article article = articleList.get(position);
        ImageUtils.setImageByBase64Str(context, holder.cover, article.cover);
        holder.title.setText(article.title);
        User authorUser = UserMapper.getUserById(context, article.authorId);
        holder.author.setText(article.author);
        ImageUtils.setImageByBase64Str(context, holder.avatar, Objects.requireNonNull(authorUser).avatar);
        holder.imageContainer.setOnClickListener(v -> {
            Toast.makeText(context, article.title, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, ArticleActivity.class);
            intent.putExtra("article_id", article.id);
            context.startActivity(intent);
        });
        // 添加点击事件
        ClickUtils.setClickToOtherActivityWithIntExtra(context, AuthorActivity.class, "author_id", article.authorId, holder.avatar, holder.author);
        ClickUtils.setupLikeButton(context, article, holder.likeButton, holder.likeCount);
        ClickUtils.setupBookButton(context, holder.bookButton);
    }

    @Override
    public int getItemCount() {
        return articleList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<Article> newArticleList) {
        articleList = newArticleList;
        notifyDataSetChanged();
    }

    public static class ArticleViewHolder extends RecyclerView.ViewHolder {
        TextView title, author, likeCount;
        ImageView cover, avatar, likeButton, bookButton;
        MaterialCardView imageContainer;

        public ArticleViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            likeCount = itemView.findViewById(R.id.like_count);
            likeButton = itemView.findViewById(R.id.like_button);
            bookButton = itemView.findViewById(R.id.book_button);
            author = itemView.findViewById(R.id.author);
            cover = itemView.findViewById(R.id.cover);
            avatar = itemView.findViewById(R.id.avatar);
            imageContainer = itemView.findViewById(R.id.image_container);
        }
    }
}
